home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / bbs / pprd199.zip / JD.PL < prev    next >
Perl Script  |  1996-06-28  |  2KB  |  62 lines

  1. #!/usr/bin/perl
  2.  
  3. # Note that if you are using this in a multitasking system, you
  4. # have to ensure that connections to pprd are serialised
  5. # so that only one jd.pl is connected at a particular time.
  6. # Exercise left to reader.
  7.  
  8. # edit this to the printer hostname
  9. $them = 'printer';
  10. $port = 9100;
  11.  
  12. open(STDIN, "$ARGV[0]") if $#ARGV >= 0;
  13.  
  14. require 'sys/socket.ph';
  15.  
  16. $sockaddr = 'S n a4 x8';
  17. chop($hostname = `hostname`);
  18.  
  19. ($name, $aliases, $proto) = getprotobyname('tcp');
  20. ($name, $aliases, $port) = getservbyname($port, 'tcp')
  21.     unless $port =~ /^\d+$/;
  22. ($name, $aliases, $type, $len, $thisaddr) = gethostbyname($hostname);
  23. ($name, $aliases, $type, $len, $thataddr) = gethostbyname($them);
  24.  
  25. socket(S, &PF_INET, &SOCK_STREAM, $proto) || &errexit("socket: $!\n");
  26.  
  27. $this = pack($sockaddr, &AF_INET, 0, $thisaddr);
  28. bind(S, $this) || &errexit("bind: $!\n");
  29.  
  30. $that = pack($sockaddr, &AF_INET, $port, $thataddr);
  31. connect(S, $that) || &errexit("connect: $!\n");
  32.  
  33. select(S); $| = 1; select(STDOUT);
  34.  
  35. $buffer = '';
  36. while (1)
  37. {
  38.     $nread = read(STDIN, $buffer, 1024);
  39.     last if $nread == 0;
  40.     &errexit("write: $!\n") unless
  41.         defined($written = syswrite(S,$buffer,$nread));
  42. }
  43. close(S);
  44. # hack to make sure server is ready before exiting and letting
  45. # spooler send next job
  46. socket(S, &PF_INET, &SOCK_STREAM, $proto) || &errexit("socket: $!\n");
  47. bind(S, $this) || &errexit("bind: $!\n");
  48. while (!connect(S, $that)) {
  49.     close(S);
  50.     sleep(5);
  51.     socket(S, &PF_INET, &SOCK_STREAM, $proto) || &errexit("socket: $!\n");
  52.     bind(S, $this) || &errexit("bind: $!\n");
  53. }
  54. close(S);
  55. exit 0;
  56.  
  57. sub errexit
  58. {
  59.     print STDERR @_;
  60.     exit 2;
  61. }
  62.